﻿using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.AP.APBill;
using UFIDA.U9.AP.ForAPI;
using UFIDA.U9.AR.ARBill;
using UFIDA.U9.Base;
using UFSoft.UBF.PL.Engine.Cache;
using UFSoft.UBF.Transactions;

namespace UFIDA.U9C.Cust.ZL.BP.DispatchBP
{
    internal class AutoGeneratePRBLogic
    {
        public static void GenPRB(APBillHead apBillHead)
        {
            UFIDA.U9.AP.ForAPI.Proxy.CreateStandardPayReqFundAPISVProxy proxy = new U9.AP.ForAPI.Proxy.CreateStandardPayReqFundAPISVProxy();
            proxy = new U9.AP.ForAPI.Proxy.CreateStandardPayReqFundAPISVProxy();
            //proxy = new U9.AP.ForAPI.Proxy.CreatePayReqFundAPISVProxy();
            proxy.PayReqFundHeadDTO = new U9.AP.ForAPI.PayReqFundStandardHeadDTOData();
            //proxy.PayReqFundHeadDTO = new PayReqFundHeadDTOData();
            proxy.PayReqFundHeadDTO.DocumentType = new U9.Base.DTOs.IDCodeNameDTOData();
            proxy.PayReqFundHeadDTO.DocumentType.Code = "001";//付款请款单
            proxy.PayReqFundHeadDTO.RequestObjType = 1;//供应商
            proxy.PayReqFundHeadDTO.ReqFundPC = new U9.Base.DTOs.IDCodeNameDTOData();
            proxy.PayReqFundHeadDTO.ReqFundPC.ID = apBillHead.FC.ID;
            //proxy.PayReqFundHeadDTO.Dept = new U9.Base.DTOs.IDCodeNameDTOData();
            //proxy.PayReqFundHeadDTO.Dept.Code = billhead.Dept;
            proxy.PayReqFundHeadDTO.PayReqFundLineDTO = new List<U9.AP.ForAPI.PayReqFundStandardLineDTOData>();
            foreach (APBillLine billline in apBillHead.APBillLines)
            {
                if ((billline.Maturity - Context.LoginDate).Days <= 30)
                {

                    proxy.PayReqFundHeadDTO.PayReqFundLineDTO.Add(new PayReqFundStandardLineDTOData
                    {
                        SrcDocLineID = billline.ID,
                        RFMoney = billline.APOCMoney.TotalMoney,
                        //SttlMethod = new U9.Base.DTOs.IDCodeNameDTOData() { Code = "YZ01" },//现金
                        //ProcessMode=

                    });
                }
            }

            if (proxy.PayReqFundHeadDTO.PayReqFundLineDTO.Count > 0)
            {
                long reqfundid = proxy.Do();
                PLCacheManager.DataCache.FlushCache();
                PLCacheManager.ObjectCache.FlushCache();
                RedisClient redisClient = new RedisClient("127.0.0.1", 6379, "ufida@123",2);

                redisClient.LPush("ZL.AUTOGEN",UTF8Encoding.UTF8.GetBytes(reqfundid.ToString()));

                //UTF8Encoding.UTF8.GetString( redisClient.RPop("ZL.AUTOGEN"));
                //PayReqFundHead payReqFundHead= PayReqFundHead.Finder.FindByID(reqfundid);

                //UFIDA.U9.AP.ForTB.Proxy.QueryReqFundHeadSrvProxy queryProxy = new UFIDA.U9.AP.ForTB.Proxy.QueryReqFundHeadSrvProxy();
                //queryProxy.QueryConditionDTO = new U9.CBO.FI.TB.DTO.TBQueryBizDocConditionDTOData();
                //queryProxy.QueryConditionDTO.OPath = $"ID={reqfundid}";
                //List<UFSoft.UBF.Business.BusinessEntity.EntityKey> entityKeys = queryProxy.Do();

                UFIDA.U9.AP.ForAPI.Proxy.PayReqFundSubmitSVProxy proxy1 = new U9.AP.ForAPI.Proxy.PayReqFundSubmitSVProxy();
                proxy1.ReqFundHeadDTOs = new List<U9.CBO.FI.DTOs.IDSysVersionDTOData>();
                proxy1.ReqFundHeadDTOs.Add(new U9.CBO.FI.DTOs.IDSysVersionDTOData()
                {
                    EntityID = reqfundid,
                    //SysVer = payReqFundHead.SysVersion
                });

                proxy1.Do();

            }

        }

    }

}
